﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace DYSales.Views.sys
{
    public partial class operator_page : Page
    {
        EditMode mode = EditMode.Noting;
        public operator_page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(operator_page_Loaded);
            bt_删除.Click += new RoutedEventHandler(bt_删除_Click);
            bt_新增.Click += new RoutedEventHandler(bt_新增_Click);
            dg_ops.SelectionChanged += new SelectionChangedEventHandler(dg_ops_SelectionChanged);
            bt_保存.Click += new RoutedEventHandler(bt_保存_Click);
        }

        void bt_保存_Click(object sender, RoutedEventArgs e)
        {
            if (g_dataform.DataContext != null)
            {
                var st = g_dataform.DataContext as Web.Operator;
                if (st.ValidationErrors != null && st.ValidationErrors.Count > 0)
                {
                    var box = other.GetMessageBox();
                    box.Title = "东雅软件错误提示";
                    box.tbk_text.Text = st.ValidationErrors.First().ErrorMessage + "数据存在不合法项，请仔细审核";
                    box.Show();
                }
                else
                {
                    if (mode == EditMode.Edit)
                    {
                        if (dm.getDS().HasChanges)
                        {
                            LayoutRoot.IsHitTestVisible = false;
                            dm.getDS().SubmitChanges(le =>
                            {
                                LayoutRoot.IsHitTestVisible = true;
                            }, null);
                        }
                    }
                    else if (mode == EditMode.Add)
                    {
                        if (tb_用户名.Text == "admin")
                        {
                            var box = other.GetMessageBox();
                            box.Title = "东雅软件";
                            box.tbk_text.Text = tb_用户名.Text + " 此用户名是超级管理员专用。";
                            box.Show();
                            return;
                        }
                        if (dm.getDS().Operators.FirstOrDefault(o => o.ot_用户名.Equals(st.ot_用户名)) == null)
                        {
                            dm.getDS().Operators.Add(st);
                            dm.getDS().SubmitChanges(lo =>
                            {
                                if (!lo.HasError)
                                {
                                    refresh();
                                }
                            }, null);
                        }
                        else
                        {
                            var box = other.GetMessageBox();
                            box.Title = "东雅软件";
                            box.tbk_text.Text = tb_用户名.Text + " 此用户名已被占用。";
                            box.Show();
                        }
                    }
                }
            }
        }

        void dg_ops_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dg_ops.SelectedItems.Count == 1)
            {
                g_dataform.DataContext = dg_ops.SelectedItem as Web.Operator;
                mode = EditMode.Edit;
                tb_用户名.IsReadOnly = true;
            }
        }

        void bt_新增_Click(object sender, RoutedEventArgs e)
        {
            var op = new Web.Operator();
            op.ot_权限组 = em_角色.收银员.ToString();
            op.ot_用户名 = string.Empty;
            op.ot_密码 = string.Empty;
            op.ot_姓名 = string.Empty;
            op.ot_启用 = true;
            g_dataform.DataContext = op;
            mode = EditMode.Add;
            tb_用户名.IsReadOnly = false;
            tb_用户名.Focus();
        }

        void bt_删除_Click(object sender, RoutedEventArgs e)
        {
            if (dg_ops.SelectedItems.Count == 1)
            {
                var op = dg_ops.SelectedItem as Web.Operator;
                if (op != null && op.ot_用户名 != "admin")
                {
                    dm.getDS().CheckSelledOp(op.ot_用户名, lo => 
                    {
                        if (!lo.Value)
                        {
                            var box = other.GetMessageBox();
                            box.Title = "东雅进销存提示";
                            box.tbk_text.Text = "确定要删除:" + op.ot_用户名 + " 操作人员吗？";
                            box.Show();
                            box.Closed += new EventHandler(box_Closed);
                        }
                        else
                        {
                            var box = other.GetMessageBox();
                            box.Title = "东雅进销存提示";
                            box.tbk_text.Text = "用户名:" + op.ot_用户名 + "已销售过商品，只能禁用该用户";
                            box.Show();
                        }
                    }, null);

                }
            }
        }

        void box_Closed(object sender, EventArgs e)
        {
            var box = sender as cwin_Question;
            box.Closed -= new EventHandler(box_Closed);
            if (box.DialogResult.Value)
            {
                var op = dg_ops.SelectedItem as Web.Operator;
                dm.getDS().Operators.Remove(op);
                dm.getDS().SubmitChanges(lo =>
                {
                    if (!lo.HasError)
                    {
                        refresh();
                    }
                }, null);
            }
        }

        void operator_page_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in Enum.GetNames(typeof(em_角色)))
            {
                cb_角色.Items.Add(item);
            }
            cb_角色.SelectedIndex = 0;

            dg_ops.Columns.Add(dgm.GetDGM().createTextColumn("角色", "ot_权限组"));
            dg_ops.Columns.Add(dgm.GetDGM().createTextColumn("用户名", "ot_用户名"));
            dg_ops.Columns.Add(dgm.GetDGM().createTextColumn("密码", "ot_密码"));
            dg_ops.Columns.Add(dgm.GetDGM().createBoolConvertTextColumn("账号是否启用", "ot_启用"));
            var d = dgm.GetDGM().createTextColumn("姓名", "ot_姓名");
            d.Width = new DataGridLength(30, DataGridLengthUnitType.Star);
            dg_ops.Columns.Add(d);

            refresh();
        }

        void refresh()
        {
            dm.getDS().Load(dm.getDS().GetOperatorsQuery(), lo =>
            {
                if (!lo.HasError)
                {
                    dg_ops.DataContext = lo.Entities.Where(o => o.ot_用户名 != "admin");
                }
            }, null);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }
}
